﻿//*****************************************************************************
//** Project:PatternWell.PAC
//** Copyright 2011 PatternWell LLC
//** Revision History:
//** Version 1.0 - Initial Release
//*****************************************************************************

using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
namespace PatternWell.PAC
{
    /// <summary>
    /// Base class used by entity objects that want to support validation and error reporting to silverlight.
    /// </summary>
    [DataContract(Namespace = GlobalInfo.PacNameSpace)]
    public abstract class ValidationEntityBase:NotificationBase,IDataErrorInfo
    {
        #region Constructor

        protected ValidationEntityBase()
        {
            _validationErrors = new Dictionary<string, string>();
        }
        #endregion

        #region Dispose Implementation - Implementation of the disposal helper method (Do not modify)

        /// <summary>
        /// Helper method that releases resources for this class implementation.
        /// </summary>
        /// <param name="disposed">flag that determines if the class has already been disposed.</param>
        protected override void Dispose(bool disposed)
        {
            ReleaseResourcesForValidationEntityBase();
            base.Dispose(disposed);
        }

        #endregion

        #region Release Management - Logic used to release resources for this class (Add release logic in the supplied helper methods)

        /// <summary>
        /// Helper method that releases resource for the 'ValidationEntityBase' class.
        /// </summary>
        private void ReleaseResourcesForValidationEntityBase()
        {
            _validationErrors = null;
        }

        #endregion

        #region IDataErrorInfo Implementation

        private Dictionary<string, string> _validationErrors = null;

        /// <summary>
        /// Returns formatted list of all the errors current found on the entity.
        /// </summary>
        public string Error
        {
            get { return FormatError(); }
        }

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <param name="columnName">The entity property to lookup</param>
        /// <returns>Error message associated with the property or an empty string if no error has occured.</returns>
        public string this[string columnName]
        {
            get 
            { 
              string validationError;
              if (!_validationErrors.TryGetValue(columnName, out validationError)) return string.Empty;
              return validationError;
            }
        }


        /// <summary>
        /// Returns all the validations errors into a single string with each error on its own line.
        /// </summary>
        /// <returns>Formatted error list or an empty string if no validation errors have occured.</returns>
        protected string FormatError()
        {
            if (_validationErrors.Count == 0) return string.Empty;

            var errorFormat = new StringBuilder();
            foreach (var error in _validationErrors)
            {
                errorFormat.AppendLine(error.Value);
            }

            return errorFormat.ToString();
        }

        /// <summary>
        /// Adds a validation error for a given entity property
        /// </summary>
        /// <param name="columnName">The name of the entity property that failed validation.</param>
        /// <param name="validationError">The validation error message for the property.</param>
        protected void AddValidationError(string columnName, string validationError)
        {
            if (_validationErrors.ContainsKey(columnName))
            {
                _validationErrors[columnName] = validationError;
            }
            else
            {
                _validationErrors.Add(columnName, validationError);
            }
        }

        /// <summary>
        /// Removes a validation error.
        /// </summary>
        /// <param name="columnName">The name of the entity property to be removed from the validation error list.</param>
        protected void RemoveValidationError(string columnName)
        {
            if(_validationErrors.ContainsKey(columnName))
            {
                _validationErrors.Remove(columnName);
            }
        }
        #endregion
    }
}
